package hw.ats.news.ui;

import hw.ats.news.R;
import hw.ats.news.activity.ats.AtsInfoAct;
import hw.ats.news.activity.news.SearchAct;
import hw.ats.news.model.Cate;
import hw.ats.news.util.App;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.humanwares.ui.tabbar.TabHost;
import com.humanwares.ui.tabbar.TabHostProvider;
import com.tattermedia.allthataudio.misc.DebugFlag;

public class CommonUI {
	private static final String TAG = "CommonUI";
	
	/**
	 * 탭바 생성
	 * @param sender
	 * @param tabId
	 * @param activityId
	 */
	public static TabHost renderTabBar(Activity sender, int tabId) {
		TabHostProvider tabProvider = new BottomTabHostProvider(sender);
		TabHost tabHost = tabProvider.getTabHost(tabId);
		tabHost.render((TableLayout)sender.findViewById(R.id.tabbar_layer));
		return tabHost;
	}
	
	/**
	 * 앱 상단의 제목
	 * @param sender
	 */
	public static void renderAppTitle(Activity sender) {
        final TextView tvTitle = (TextView)sender.findViewById(R.id.navbar_title);
//        tvTitle.setText(R.string.app_nav_title);
        tvTitle.setText(App.APP_TITLE);
	}
	/**
	 * 제목과 분류바가 있는 서브네비게이션바 생성
	 * @param sender
	 * @param categoryArea
	 * @param cates
	 */
	public static CateNavHolder makeWithCategorizedSubNavigation(final Activity sender, 
			List<Cate> cates, int cateUid) {
		if (cates == null) return null;
		
		final CateNavHolder cateNavHolder = new CateNavHolder(cates);
		
		ImageButton prevCatBtn = (ImageButton)sender.findViewById(R.id.navcat_prev_btn);
		cateNavHolder.prevBtn = prevCatBtn;
		prevCatBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				HorizontalScrollView sv = (HorizontalScrollView)sender.findViewById(R.id.navcat_scroll);
				sv.smoothScrollBy(-100, 0);
			}
		});
		
		ImageButton nextCatBtn = (ImageButton)sender.findViewById(R.id.navcat_next_btn);
		cateNavHolder.nextBtn = nextCatBtn;
		nextCatBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				HorizontalScrollView sv = (HorizontalScrollView)sender.findViewById(R.id.navcat_scroll);
				sv.smoothScrollBy(100, 0);
			}
		});
		
		// 카테고리 출력
		int selectedPosition = -1;
		LinearLayout topcatbar = (LinearLayout)sender.findViewById(R.id.topcatbar_list);
        for (int i = 0, len = cates.size(); i < len; i++) {
		  	final Cate cate0 = cates.get(i);
		  	if (cateUid == cate0.uid)
		  		selectedPosition = i; // 선택된 위치
	        final Button textView0 = new Button(sender);
	        cateNavHolder.views.add(textView0);
	        String title = cate0.title.replaceAll("[\\[\\]]+", "");
	        textView0.setText(" "+title+" ");
	        textView0.setTextColor(0xff666666);
	        textView0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
	        //textView0.setPadding(10, 0, 10, 0);
	        textView0.setTag(Integer.valueOf(i));
	        textView0.setGravity(Gravity.CENTER_VERTICAL);
        	textView0.setBackgroundResource(R.drawable.bg_catbar);
	        textView0.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					CateNavCallback cb = (CateNavCallback)sender;
					int position = (Integer)textView0.getTag();
					cb.onSelectCateNav(position, cateNavHolder);
				}
			});
	        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
	                LinearLayout.LayoutParams.WRAP_CONTENT,
	                LinearLayout.LayoutParams.WRAP_CONTENT);
	        layoutParams.setMargins(0, 0, 10, 0);
	        topcatbar.addView(textView0, layoutParams);
        }
        
        if (selectedPosition >= 0)
        	cateNavHolder.select(selectedPosition);
        	
        if (DebugFlag.isLogEnabled())
        	Log.d(TAG, "select_pos=" + selectedPosition);
        
        return cateNavHolder;
	}
	
	/*
	public static CateNavHolder makeWithCategorizedSubNavigation(final Activity sender, 
			List<Cate> cates, int cateUid) {
		if (cates == null) return null;
		
		final Button[] buttons = new Button[cates.size()];
		final CateNavHolder cateNavHolder = new CateNavHolder(cates);
		
		ImageButton prevCatBtn = (ImageButton)sender.findViewById(R.id.navcat_prev_btn);
		cateNavHolder.prevBtn = prevCatBtn;
		prevCatBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				HorizontalScrollView sv = (HorizontalScrollView)sender.findViewById(R.id.navcat_scroll);
				sv.smoothScrollBy(-50, 0);
			}
		});
		
		ImageButton nextCatBtn = (ImageButton)sender.findViewById(R.id.navcat_next_btn);
		cateNavHolder.nextBtn = nextCatBtn;
		nextCatBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				HorizontalScrollView sv = (HorizontalScrollView)sender.findViewById(R.id.navcat_scroll);
				sv.smoothScrollBy(50, 0);
			}
		});
		
		// 카테고리 출력
		LinearLayout topcatbar = (LinearLayout)sender.findViewById(R.id.topcatbar_list);
        for (int i = 0, len = cates.size(); i < len; i++) {
		  	final Cate cate0 = cates.get(i);
		  	buttons[i] = new Button(sender);
//	        String title = cate0.title.replaceAll("[\\[\\]]+", "");
	        cateNavHolder.views.add(buttons[i]);
	        buttons[i].setText(cate0.title);
	        buttons[i].setTextColor(0xff000000);
	        buttons[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
	        buttons[i].setPadding(0, 0, 0, 0);
	        buttons[i].setTag(Integer.valueOf(i));
	        //buttons[i].setGravity(Gravity.CENTER_VERTICAL);
	        buttons[i].setBackgroundResource(R.drawable.ic_navcat_bg);
	        buttons[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					CateNavCallback cb = (CateNavCallback)sender;
					int position = (Integer)v.getTag();
					cb.onSelectCateNav(position, cateNavHolder);
				}
			});
	        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
	                LinearLayout.LayoutParams.WRAP_CONTENT,
	                LinearLayout.LayoutParams.WRAP_CONTENT
	                );
	        layoutParams.setMargins(0, 0, 10, 0);
	        topcatbar.addView(buttons[i], layoutParams);
        }
        
        return cateNavHolder;
	}
	 */
	
	/**
	 * 좌측 네비게이션 버튼을 검색화면으로 이동하도록 이벤트 부여
	 * @param sender
	 */
	public static void addLeftNavigationButtonSearchEvent(final Activity sender) {
        ImageButton leftImageButton = (ImageButton)sender.findViewById(R.id.navbar_left);
        if (leftImageButton != null) {
        	leftImageButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(sender, SearchAct.class);
					sender.startActivity(intent);
				}
			});
        }
	}
	
	/**
	 * 좌측 네비게이션 버튼을 이전화면으로 이동하도록 이벤트 부여
	 * @param sender
	 */
	public static void addLeftNavigationButtonBackEvent(final Activity sender) {
        ImageButton leftImageButton = (ImageButton)sender.findViewById(R.id.navbar_left);
        if (leftImageButton != null) {
        	leftImageButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					sender.finish();
				}
			});
        }
	}
	
	/**
	 * 우측 네비게이션 버튼을 All That 화면으로 이동하도록 이벤트 부여
	 * @param sender
	 */
	public static void addRightNavigationButtonAllThatEvent(final Activity sender) {
        ImageButton rightImageButton = (ImageButton)sender.findViewById(R.id.navbar_right);
        if (rightImageButton != null) {
        	rightImageButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(sender, AtsInfoAct.class);
					sender.startActivity(intent);
				}
			});
        }
	}
}
